using System.Collections;
using System.Collections.Generic;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    public class GamePauseView : View
    {
        private Button mbtnClose;
        
        private GameObject mobjPanel1;
        private Toggle mtogMusic;
        private GameObject mobjMusicOpen;
        private GameObject mobjMusicClose;
        private Toggle mtogSound;
        private GameObject mobjSoundOpen;
        private GameObject mobjSoundClose;
        public Button mbtnReGame;
        public Button mbtnExitGame;

        private GameObject mobjPanel2;
        public Button mbtnExitGame2;
        
        
        public override void OnLoad()
        {
            base.OnLoad();
            
            mbtnClose = transform.Find("btnclose").GetComponent<Button>();

            mobjPanel1 = transform.Find("backone").gameObject;
            mtogMusic = transform.Find("backone/yinxiao/music").GetComponent<Toggle>();
            mobjMusicOpen = transform.Find("backone/yinxiao/music/open").gameObject;
            mobjMusicClose = transform.Find("backone/yinxiao/music/close").gameObject;
            mtogSound = transform.Find("backone/yinxiao/sound").GetComponent<Toggle>();
            mobjSoundOpen = transform.Find("backone/yinxiao/sound/open").gameObject;
            mobjSoundClose = transform.Find("backone/yinxiao/sound/close").gameObject;
            
            mbtnReGame = transform.Find("backone/btnretry").GetComponent<Button>();
            mbtnExitGame = transform.Find("backone/btnexit").GetComponent<Button>();

            
            mobjPanel2 = transform.Find("backtwo").gameObject;
            mbtnExitGame2 = transform.Find("backtwo/btnexit").GetComponent<Button>();
            
            OnEvent();
        }

        private void OnEvent()
        {
            AddClickListener(mbtnClose, () =>
            {
                GameHelp.CanRayCast = true;
                GameHelp.GamePause = false;
                UIManager.Instance.Close(ViewName.GamePauseView);
            });
            mtogMusic.onValueChanged.AddListener((value_) =>
            {
                AudioSystem.Instance.OnPlayBtn();
                if (value_)
                {
                    AudioSystem.Instance.ResumeBgm();
                    GameLocalInfo.OnSetIsPlayBGM(true);
                }
                else
                {
                    AudioSystem.Instance.PauseBgm();
                    GameLocalInfo.OnSetIsPlayBGM(false);
                }
                mobjMusicOpen.SetActive(value_);
                mobjMusicClose.SetActive(!value_);
            });
           
            mtogSound.onValueChanged.AddListener((value_) =>
            {
                AudioSystem.Instance.OnPlayBtn();
                if (value_)
                {
                    AudioSystem.Instance.SetEffectAudioVolume(1);
                    GameLocalInfo.OnSetIsPlayEffect(true);
                }
                else
                {
                    AudioSystem.Instance.SetEffectAudioVolume(0);
                    GameLocalInfo.OnSetIsPlayEffect(false);
                }
                mobjSoundOpen.SetActive(value_);
                mobjSoundClose.SetActive(!value_);
            });
            
            AddClickListener(mbtnReGame, () =>
            {
                if (GameLocalInfo.mPlayerPower <= 0)
                {
                    UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, "体力不足！");
                }
                else
                {
                    UIManager.Instance.Close(ViewName.GamePauseView);

                    GameLocalInfo.OnUpdatePlayerPower(-1);
                    // GameHelp.GameLevel = 1;
                    EventCenter.BroadCast(EventUtils.EventType.EVENT_GAME_START);
                }
            });
            AddClickListener(mbtnExitGame, () =>
            {
                mobjPanel1.SetActive(false);
                mobjPanel2.SetActive(true);
            });
            
            AddClickListener(mbtnExitGame2, () =>
            {
                MJCtrl.Instance().OnDesctroyCard();
                UIManager.Instance.Close(ViewName.GamePauseView);
                UIManager.Instance.Close(ViewName.Game1View);
                UIManager.Instance.Close(ViewName.Game2View);
                UIManager.Instance.Show(ViewName.LobbyView);
            });
        }
        
        public override void OnShow(params object[] args)
        {
            base.OnShow();
            mtogMusic.isOn = GameLocalInfo.OnIsPlayBGM();
            mobjMusicOpen.SetActive(mtogMusic.isOn);
            mobjMusicClose.SetActive(!mtogMusic.isOn);
            
            mtogSound.isOn = GameLocalInfo.OnIsPlayEffect();
            mobjSoundOpen.SetActive(mtogSound.isOn);
            mobjSoundClose.SetActive(!mtogSound.isOn);
            
            mobjPanel1.SetActive(true);
            mobjPanel2.SetActive(false);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
        }

    }
}

